package com.suntine.familytree.businessunit.login.controller;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.suntine.familytree.businessunit.login.assistant.LoginForm;
import com.suntine.familytree.businessunit.regiester.service.RegisterService;
import com.suntine.familytree.core.business.user.entity.User;
import com.suntine.familytree.core.framework.controller.BaseController;
import com.suntine.familytree.core.framework.util.AjaxUtils;
/**
 * @corporation 
 * @author suntine
 * @creation 2011-3-30 下午01:21:55
 * @description 
 */

@Controller
@RequestMapping(value="/login")
public class LoginController extends BaseController{
	
	
	    @Autowired
	    private RegisterService registerService;
	    
		public LoginController() {
			super();
			// TODO Auto-generated constructor stub
			this.initLogger(this.getClass());
		}

		@RequestMapping(value="",method=RequestMethod.GET) //访问路径
		public String loginPage(@RequestHeader(value="X-Requested-With", required=false) String requestedWith,Model model,HttpSession session){
			model.addAttribute("loginForm", new LoginForm());
			model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
			List list = registerService.queryAll();
			this.logger.debug("目前有多少个学员？"+list.size()+"个");
			return "/login/login"; //页面路径
		}
		
		@RequestMapping(value="validate",method=RequestMethod.POST)
		public String loginValidate(@RequestHeader(value="X-Requested-With", required=false) String requestedWith,@ModelAttribute("loginForm") @Valid LoginForm loginForm, BindingResult result, HttpSession session,Model model){
			
			if (result.hasErrors()) {
				model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
				return "/login/login";
			}else{
				User user = new User();
				user.setRealName("sunxiaozhou");
				model.addAttribute("user",user);
				session.setAttribute("userId", loginForm.getUserId());
				return "/pure_success";
			}
		}
		
}